//
//  BKFloatAboveKeyboardView.m
//  XPhoto
//
//  Created by baboy on 2019/7/26.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "BKFloatAboveKeyboardView.h"
@interface BKFloatAboveKeyboardView()
@property(nonatomic, strong) UIView *topView;
@property(nonatomic, assign) CGRect keyboardBounds;
@property(nonatomic, assign) CGFloat animationDuration;
@property(nonatomic, assign) NSInteger animationCurve;
@property(nonatomic, assign) CGRect originBounds;
@end

@implementation BKFloatAboveKeyboardView
- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addKeyboardOBserver];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self addKeyboardOBserver];
    }
    return self;
}
- (void)awakeFromNib{
    [super awakeFromNib];
    self.originBounds = self.bounds;
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)addKeyboardOBserver{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}
- (UIView *)topView{
    if (!_topView) {
        UIView *v = self;
        while (v.superview && v.superview != APP_KEY_WIN) {
            v = v.superview;
        }
        _topView = v;
    }
    return _topView;
}
#pragma keyboard listener
- (void)keyboardWillShow:(NSNotification *)notification{
    CGRect keyboardBounds;
    [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    self.animationCurve = [curve intValue];
    self.animationDuration = [duration doubleValue];
    
    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self convertRect:keyboardBounds toView:nil];
    self.keyboardBounds = keyboardBounds;
    [self layoutSubviewsWithKeyboardPopup:YES];
}
- (void)keyboardWillHide:(NSNotification *)notification{
    [self layoutSubviewsWithKeyboardPopup:NO];
}

- (void)layoutSubviewsWithKeyboardPopup:(BOOL)keyboardPopup{
    CGRect frame = self.frame;
    frame.size.height = self.originBounds.size.height +(keyboardPopup?0:self.topView.safeAreaInsets.bottom);
    frame.origin.y = self.topView.bounds.size.height - (keyboardPopup? self.keyboardBounds.size.height:0) - frame.size.height;
    frame = [self.topView convertRect:frame toView:self.superview];
    frame.origin.x = self.frame.origin.x;
    
    // animations settings
    [UIView animateWithDuration:self.animationDuration
                          delay:0
                        options:self.animationCurve
                     animations:^{
                         self.frame = frame;
                     }
                     completion:^(BOOL finished) {
                     }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
